/*
 * @Description:
 * @Author: chenjiaxin
 * @Date: 2022-02-02 12:18:42
 * @LastEditTime: 2022-02-06 16:32:33
 * @FilePath: \super-unit\lib\time\date.js
 */
let typeTool = require("../type-tool/index.js");
// import typeTool from "../type-tool/index.js";

/**
 * 日期时间 转 xxxx 年xx月xx日 时分秒
 * @param {*} date 日期格式
 * @param {*} type
 * @returns
 */
const dateToString = (date, type = "YYYY-MM-DD hh:mm:ss") => {
  // console.log("日期", date);
  date = typeTool.isDate(date) ? date : new Date();

  let y = date.getFullYear();
  let m = (date.getMonth() + 1).toString().padStart(2, 0);
  let d = date.getDate().toString().padStart(2, 0);
  let h = date.getHours().toString().padStart(2, 0);
  let min = date.getMinutes().toString().padStart(2, 0);
  let s = date.getSeconds().toString().padStart(2, 0);

  let typeArr = type.split(" ");
  let ymd = typeArr[0];
  let hms = typeArr[1];

  let dateString;
  if (ymd === "YYYY-MM-DD") {
    dateString = `${y}-${m}-${d}`;
  } else {
    dateString = `${y}/${m}/${d}`;
  }

  if (hms && hms === "hh:mm:ss") {
    dateString += ` ${h}:${min}:${s}`;
  }

  return dateString;
};

/**
 * 时间戳转 字符串日期时间
 * @param {*} value 时间戳
 * @returns
 */
const timestampToDateTime = (value) => {
  if (value.length === 13) {
    value = Number(value);
  } else if (value.length === 10) {
    value = Number(value) * 1000;
  } else {
    throw "格式错误";
  }
  let date = new Date(value);
  return dateToString(date);
};

module.exports = {
  dateToString,
  timestampToDateTime
};

// export {
//   dateToString
// }
